<?php
namespace App\Services\Sp\Sp;

use App\Models\Sp\Sp;
use App\Models\Agent\Agent;
use App\Models\Sale\Sale;


class SpLogic
{
    // 添加业务员
    public function addSp($data)
    {
//        $data = init_array($data,[
//            'sp_type' => '',
//            'full_name' => '',
//            'simple_name' => '',
//            'card_type' => 0,
//            'card' => '',
//            'phone_name' => '',
//            'phone' => '',
//            'weixin' => '',
//            'weibo' => '',
//            'email' => '',
//            'url' => '',
//            'memory_des' => 0,
//            'check_cycle' => 0,
//        ]);
//        $fixChoose = 0;
//        switch ($data['fix_choose']) {
//            case 1:
//                $fixChoose = 1;
//                break;
//            case 2:
//                $fixChoose = 2;
//                break;
//            case 3:
//                $fixChoose = 3;
//                break;
//            case 4:
//                $fixChoose = 5;
//                break;
//            case 5:
//                $fixChoose = 7;
//                break;
//            case 6:
//                $fixChoose = 10;
//                break;
//        }
       // $data['expired_time'] = date('Y-m-d',strtotime($data['fix_date'].' + '.$fixChoose.' year'));
        if($data['card_type']==1 && strlen($data['card'])!=18) {
            return verify_err(null, '身份证号为18位');
        }
        $data['memory_day'] = [];
        if(!empty($data['memory_day1']))
            $data['memory_day'][] = ['day' => $data['memory_day1'], 'des' => $data['memory_des1']];

        if(!empty($data['memory_day2']))
            $data['memory_day'][] = ['day' => $data['memory_day2'], 'des' => $data['memory_des2']];

        if(!empty($data['memory_day3']))
            $data['memory_day'][] = ['day' => $data['memory_day3'], 'des' => $data['memory_des3']];

        $data['memory_day'] = serialize($data['memory_day']);
        unset($data['memory_day1']);
        unset($data['memory_des1']);
        unset($data['memory_day2']);
        unset($data['memory_des2']);
        unset($data['memory_day3']);
        unset($data['memory_des3']);

        if(array_key_exists('token',$data))
            unset($data['token']);
        $rst = Sp::insertGetId($data);
        if ($rst === false) {
            return auto_failure($data, '创建sp成功！');
        } elseif ($rst === 0) {
            return auto_failure($data, '创建sp失败！');
        }
        return auto_return(['id' => $rst],'创建sp成功！');
    }

    //sp列表
    public function spList($data){
        $query = Sp::orderBy('id','asc');
        !empty($data['simple_name']) && $query->where('simple_name', 'like', '%'.$data['simple_name'].'%');
        !empty($data['phone_name']) && $query->where('phone_name', 'like', '%'.$data['phone_name'].'%');
        !empty($data['fix_date']) && $query->where('fix_date', '=', $data['fix_date']);
        !empty($data['main']) && $query->where('main', '=', $data['main']);
        $res = $query->paginate(10);
        return auto_return($res,'成功！');
    }

    //sp详情
    public function spDetail($data){
        $res = Sp::where('id','=',$data['id'])->get()->toArray();
        $res[0]['memory_day'] = unserialize($res[0]['memory_day']);

        //查询业务员手机号
        $mainSale = Sale::where('name', '=', $res[0]['main'])->get()->toArray();
        $assistSale = Sale::where('name', '=', $res[0]['assist'])->get()->toArray();
        if($mainSale){
            $res[0]['main_phone'] = $mainSale[0]['phone'];
        }else{
            $res[0]['main_phone'] = '无';
        }
        if($assistSale){
            $res[0]['assist_phone'] = $assistSale[0]['phone'];
        }else{
            $res[0]['assist_phone'] = '无';
        }
        return auto_return($res,'成功!');
    }

    //修改sp
    public function updateSp($data){
        $res = Sp::where('id','=',$data['id'])->update($data);
        return auto_return($res,'成功!');
    }

    //终止/恢复协议
    public function termination($postData)
    {
        $data = Sp::find($postData['id']);
        $data->status = $postData['status'];

        $res = $data->save();
        if ($res) {
            return auto_return($data);
        } else {
            return auto_failure('操作失败');
        }
    }
   
}